<?php namespace App\Http\Controllers\Api;

use App\Department;
use App\Hospital;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function setHospital(Request $request,Guard $guard){
        if($guard->guest())
            return response()->json(['level'=>'error',"message"=>'不允许访问']);

        $id        = !is_null($request->get('id')) ? $request->get('id') : 0;
        $name      = $request->get('name');
        $telephone = $request->get('telephone');

        /** @var Hospital $thatHospital */
        $thatHospital = Hospital::whereId($id)->first();
        if(!$thatHospital){
            $thatHospital = new Hospital();
        }

        $thatHospital->name = $name;
        $thatHospital->telephone = $telephone;
        $thatHospital->save();

        return response()->json(['level'=>'success','message'=>'添加成功',"data"=>$thatHospital]);
    }

    public function delHospital(Request $request,Guard $guard){
        if($guard->guest())
            return response()->json(['level'=>'error',"message"=>'不允许访问']);

        $id = $request->get('id');
        /** @var Hospital $thatHospital */
        $thatHospital = Hospital::whereId($id)->first();
        if(!$thatHospital){
            return response()->json(['level'=>'error',"message"=>'当前医院不存在']);
        }

        $thatHospital->delete();
        return response()->json(['level'=>'success','message'=>'删除成功',"data"=>$thatHospital]);
    }

    public function setHospitalDep(Request $request,Guard $guard){
        if($guard->guest())
            return response()->json(['level'=>'error',"message"=>'不允许访问']);

        $hospitalId = $request->get('hospital_id');
        $depName    = $request->get('dep_name');

        /** @var Hospital $thatHospital */
        $thatHospital = Hospital::whereId($hospitalId)->first();
        if(!$thatHospital)
            return response()->json(['level'=>'error','message'=>'当前医院不存在']);

        $dep = new Department();
        $dep->dep_name    = $depName;
        $dep->hospital_id = $thatHospital->id;
        $dep->save();

        return response()->json(['level'=>'success','message'=>'添加成功',"data"=>$dep]);
    }

    public function delHospitalDep(Request $request,Guard $guard){
        if($guard->guest())
            return response()->json(['level'=>'error',"message"=>'不允许访问']);

        $id = $request->get('id');

        /** @var Department $thatHospitalDep */
        $thatHospitalDep = Department::whereId($id)->first();
        if(!$thatHospitalDep){
            return response()->json(['level'=>'error',"message"=>'当前科室不存在']);
        }
        $thatHospitalDep->delete();

        return response()->json(['level'=>'success','message'=>'删除成功',"data"=>$thatHospitalDep]);
    }

    public function setHospitalSort(Guard $guard,Request $request){
        if($guard->guest())
            return redirect('Admin.Index.login');

        $id = $request->get('id');

        $theHospital = Hospital::whereId($id)->first();
        if($theHospital){
            $theHospital->sort = time();
            $theHospital->save();
        }

        return response()->json(['level'=>'success','message'=>'置顶成功',"data"=>$theHospital]);
    }
}